package mtn.bowen.yan.server.fx;

import com.jfoenix.controls.JFXProgressBar;
import javafx.application.Preloader;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import mtn.bowen.yan.server.utils.ResourceBundleUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.ResourceBundle;

/**
 * @author <a href=mailto:yanbowen90@foxmail.com> bowen.yan </a>
 * @version 1.0
 * @description: TODO
 * @date 2021-10-21 11:57 PM
 */
public class FxAppPreloader extends Preloader {

    private final static Logger LOGGER = LoggerFactory.getLogger(FxAppPreloader.class);

    private JFXProgressBar progressBar;
    private Parent view;
    private Stage stage;
    private Label label;

    @Override
    public void init() {
        try {
            ResourceBundle bundle = ResourceBundleUtil.getResource();
            view = FXMLLoader.load(getClass().getResource("/template/loader/loader.fxml"), bundle);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void start(Stage primary) {
        stage = primary;
        primary.initStyle(StageStyle.TRANSPARENT);
        Scene scene = new Scene(view);
        scene.setFill(Color.TRANSPARENT);
        scene.getStylesheets().add(getClass().getResource("/styles/theme/fonts.css").toExternalForm());
        progressBar = (JFXProgressBar) scene.lookup("#progressBar");
        label = (Label) scene.lookup("#progressLabel");
        primary.getIcons().add(new Image("/icons/icon.png"));
        primary.setScene(scene);
        primary.setAlwaysOnTop(true);
        primary.show();

    }

    @Override
    public synchronized void handleApplicationNotification(Preloader.PreloaderNotification info) {
        if (info instanceof Preloader.ProgressNotification) {
            double x = ((Preloader.ProgressNotification) info).getProgress();

            double percent = x / 100f;
            progressBar.progressProperty().set(percent > 1 ? 1 : percent);
        }
    }

    @Override
    public void handleStateChangeNotification(StateChangeNotification info) {
        try {
            StateChangeNotification.Type type = info.getType();
            switch (type) {
                case BEFORE_LOAD:
                    label.textProperty().set("初始化成功...");
                    Thread.sleep(2000);
                    break;
                case BEFORE_INIT:
                    label.textProperty().set(ResourceBundleUtil.getStringValue("loader.loading.modules"));
                    Thread.sleep(1000);
                    break;
                case BEFORE_START:
                    label.textProperty().set("加载成功，即将跳转到主页面");
                    Thread.sleep(1000);
                    stage.close();
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

